<?php
/**
 *
 */

class RPHP_Db_Mysqli extends RPHP_Db_Abstract
{
    /**
     * Connect to MySQL
     *
     * @return resource connection
     */
    protected function _connect($params)
    {

        $mysqli = new mysqli(
            $params['host'],
            $params['user'],
            $params['password'],
            $params['database'],
            $params['port']
        );
        if (!$mysqli->connect_errno) {
            $mysqli->query("SET NAMES " . $this->_config['charset']);
            $mysqli->query("SET AUTOCOMMIT " . $this->_config['autocommit']);
            return $mysqli;
        }

    }

    /**
     * Close mysql connection
     *
     */
    public function close()
    {
        $this->_connection->close();
    }

    /**
     * Query sql
     *
     * @param string $sql
     * @return Cola_Com_Db_Mysql
     */
    public function query($sql)
    {
        $this->_lastSql = $sql;
        if ($this->_query = $this->_connection->query($sql)) {
            if (!$this->_connection->errno) {
                return $this;
            }
        }

        if ($this->error_reporting && $this->_connection->error && $this->_connection->errno) {
            echo "Mysql Error Infomation\nERROR[" . $this->_connection->errno . "] " . $this->_connection->error . "\nSQL:" . $sql . "\n";
        }

    }

    public function getnum($sql)
    {
        $this->query($sql);
        return $this->_query->num_rows;

    }

    public function getrow($sql)
    {
        $this->query($sql);

        $result = $this->fetchAll();

        if ($result) {
            return $result[0];
        } else {
            return false;
        }

    }

    public function getone($sql)
    {

        $data = $this->getrow($sql);
        if ($data) {
            foreach ($data as $k => $r) {
                return $data[$k];
            }
        } else {
            return false;
        }

    }

    /**
     * Return the rows affected of the last sql
     *
     * @return int
     */
    public function affectedRows()
    {
        return $this->_connection->affected_rows;
    }

    /**
     * Fetch one row result
     *
     * @param string $type
     * @return mixd
     */
    public function fetch($type = 'ASSOC')
    {
        $type = strtoupper($type);

        switch ($type) {
            case 'ASSOC':
                $func = 'fetch_assoc';
                break;
            case 'NUM':
                $func = 'fetch_array';
                break;
            case 'OBJECT':
                $func = 'fetch_object';
                break;
            default:
                $func = 'fetch_assoc';
        }

        return $this->_query->$func();
    }

    /**
     * Fetch All result
     *
     * @param string $type
     * @return array
     */
    public function fetchAll($type = 'ASSOC')
    {
        switch ($type) {
            case 'ASSOC':
                $func = 'fetch_assoc';
                break;
            case 'NUM':
                $func = 'fetch_array';
                break;
            case 'OBJECT':
                $func = 'fetch_object';
                break;
            default:
                $func = 'fetch_assoc';
        }
        $result = array();
        if ($this->_query) {
            while ($row = $this->_query->$func()) {
                $result[] = $row;
            }
            $this->_query->free();
        }
        return $result;
    }

    /**
     * Get last insert id
     *
     * @return int
     */
    public function lastInsertId()
    {
        return $this->_connection->insert_id;
    }

    /**
     * Beging transaction
     *
     */
    public function begin()
    {
        $this->query('START TRANSACTION');
    }

    /**
     * Commit transaction
     *
     * @return boolean
     */
    public function commit()
    {
        if ($result = $this->query('COMMIT')) {
            return true;
        }

    }

    /**
     * Roll back transaction
     *
     * @return boolean
     */
    public function rollback()
    {
        if ($result = $this->query('ROLLBACK')) {
            return true;
        }

    }

    /**
     * Get error
     *
     * @return string|array
     */
    public function error($type = 'STRING')
    {
        $type = strtoupper($type);

        $errno = $mysqli->errno;
        $error = $mysqli->error;

        if ('ARRAY' == $type) {
            return array('code' => $errno, 'msg' => $error);
        }
        return $errno . ':' . $error;
    }
}
